import { defineStore } from "pinia"
import {removeTime} from "@/utils/format"
import {
  postUserListRequest,
  deleteUserRequest,
  newUserCreate,
  editUserData,
  postPageListData,
  deletePageByIdRequest,
  newPageDataRequest,
  editPageDataRequest
} from "@/service/main/system/system"
import useMainStore from "../main"
interface ISystemState {
  usersList: any[]
  totalCount: number
  roleList: any[]
  pageList: any[]
  pageTotalCount: number
}

const useSystemStore = defineStore("system", {
  state: (): ISystemState => ({
    usersList: [],
    totalCount: 0,
    roleList: [],
    pageList: [],
    pageTotalCount: 0
  }),
  actions: {
    async postUserListAction(queryInfo: any) {
      const userList = await postUserListRequest(queryInfo)
      // console.log(userList.data.list)
      const { totalCount, list } = userList.data
      this.usersList = removeTime(list)
      this.totalCount = totalCount
    },
    async deleteUserAction(id: number) {
      const result = await deleteUserRequest(id)
      // console.log(result)
      this.postUserListAction({ size: 10, offset: 0 })
    },
    async newUserDataAction(userInfo: any) {
      console.log(userInfo)
      const newResult = await newUserCreate(userInfo)
      // 更新请求新的数据
      this.postUserListAction({ size: 10, offset: 0 })
      //  console.log(newResult)
      //获取完整的数据
      const mainStore=useMainStore()
      mainStore.fetchEntireDataAction()
    },
    // 编辑用户数据
    async editUserDataAction(id: number, userInfo: any) {
      const editResult = await editUserData(id, userInfo)
      // console.log(editResult)
      this.postUserListAction({ size: 10, offset: 0 })
    },
    // 针对页面数据,增删改查
    async postPageListAction(pageName: string, queryInfo: any) {
      const PageListResult = await postPageListData(pageName, queryInfo)
      const { totalCount, list } = PageListResult.data
      this.pageList = removeTime(list)
      this.pageTotalCount = totalCount
    },
    async deletePageByIdAction(pageName: string, id: number) {
      const pageByIdReult = await deletePageByIdRequest(pageName, id)
      console.log(pageByIdReult)
      this.postPageListAction(pageName, { size: 10, offset: 0 })
        //获取完整的数据
        const mainStore=useMainStore()
        mainStore.fetchEntireDataAction()
    },
    async newPageDataAction(pageName:string,userInfo:any){
      const pageDataReult=await newPageDataRequest(pageName,userInfo)
      console.log(pageDataReult)
      this.postPageListAction(pageName, { size: 10, offset: 0 })
        //获取完整的数据
        const mainStore=useMainStore()
        mainStore.fetchEntireDataAction()
    },
    async editPageDataDataAction(pageName:string,id:number,userInfo:any){
      const editPageReult=await editPageDataRequest(pageName,id,userInfo)
      console.log(editPageReult)
      this.postPageListAction(pageName, { size: 10, offset: 0 })
        //获取完整的数据
        const mainStore=useMainStore()
        mainStore.fetchEntireDataAction()
    }
  }
})

export default useSystemStore
